﻿using System.Collections.Generic;
using System.Data;
using Ue.Infrastructure.RepositoryFramework;
using Ue.Qcdz.Domain.Shops;
using Ue.Qcdz.Domain.ShopSinglePages;

namespace Ue.Qcdz.Repositories.ShopSinglePages
{
    public class ShopSinglePageRepository : SqlServerRepository<ShopSinglePage>, IShopSinglePageRepository
    {
        protected override System.Data.IDataParameter[] BuildParameters(ShopSinglePage entity)
        {
            return new IDataParameter[]{
              this.RepositoryDriver.BuildParameter(ShopSinglePageFactory.FieldNames.EntityKey,entity.Key),
              this.RepositoryDriver.BuildParameter(ShopSinglePageFactory.FieldNames.Title,entity.Title??""),              
              this.RepositoryDriver.BuildParameter(ShopSinglePageFactory.FieldNames.ImageUrl,entity.ImageUrl??""),   
              this.RepositoryDriver.BuildParameter(ShopSinglePageFactory.FieldNames.Content,entity.Content??""), 
              this.RepositoryDriver.BuildParameter(ShopSinglePageFactory.FieldNames.Status,entity.Status),              
              this.RepositoryDriver.BuildParameter(ShopSinglePageFactory.FieldNames.Remark,entity.Remark??""),
              this.RepositoryDriver.BuildParameter(ShopSinglePageFactory.FieldNames.ShopId,entity.Shop.Key),
              this.RepositoryDriver.BuildParameter(ShopSinglePageFactory.FieldNames.DisplayOrder,entity.DisplayOrder),
              this.RepositoryDriver.BuildParameter(ShopSinglePageFactory.FieldNames.CreateOn,entity.CreateOn)
  
            };
        }

        protected override Infrastructure.RepositoryFramework.Repository.IWhereClause BuildWhereClauseBy(Infrastructure.DomainBase.IDataQuery dataQuery)
        {
            if (dataQuery is IShopSinglePageDataQuery)
            {
                return new ShopSinglePageDataQueryWhereClause(dataQuery as IShopSinglePageDataQuery);
            }

            return null;
        }

        protected override string[] GetBaseInsertFields()
        {
            return new string[] {
                    ShopSinglePageFactory.FieldNames.EntityKey,
                    ShopSinglePageFactory.FieldNames.Title,
                    ShopSinglePageFactory.FieldNames.ImageUrl,   
                    ShopSinglePageFactory.FieldNames.Content,
                    ShopSinglePageFactory.FieldNames.ShopId,
                    ShopSinglePageFactory.FieldNames.Remark,
                    ShopSinglePageFactory.FieldNames.Status, 
                    ShopSinglePageFactory.FieldNames.DisplayOrder,
                    ShopSinglePageFactory.FieldNames.CreateOn
     
            };
        }

        protected override string[] GetBaseUpdateFields()
        {
            return new string[] {
                    ShopSinglePageFactory.FieldNames.Title,
                    ShopSinglePageFactory.FieldNames.ImageUrl,   
                    ShopSinglePageFactory.FieldNames.Content,
                    ShopSinglePageFactory.FieldNames.ShopId,
                    ShopSinglePageFactory.FieldNames.Remark,
                    ShopSinglePageFactory.FieldNames.Status, 
                    ShopSinglePageFactory.FieldNames.DisplayOrder
            };
        }

        protected override IDictionary<string, Infrastructure.RepositoryFramework.Repository.SqlServerRepositoryBase<ShopSinglePage>.ChildDataHandler> GetBuildEntityChildCallbacks()
        {
            var callBacks = new Dictionary<string, SqlServerRepository<ShopSinglePage>.ChildDataHandler>();
            callBacks.Add(ShopSinglePageFactory.FieldNames.ShopId, delegate(ShopSinglePage shopSinglePage, object fieldValue)
            {
                ShopSinglePageFroxy shopSinglePageProxy = shopSinglePage as ShopSinglePageFroxy;
                if (shopSinglePageProxy != null)
                {
                    IShopRepository shopRepository = RepositoryFactory.GetRepository<IShopRepository, Shop>();
                    shopSinglePageProxy.Shop = shopRepository[fieldValue];
                }
            });
            return callBacks;
        }

        protected override string GetDataTableName()
        {
            return "ShopSinglePage";
        }

        #region IShopSinglePageRepository 成员

        public ShopSinglePage GetSinglePage(string title, Shop shop)
        {
            if (string.IsNullOrEmpty(title) || shop == null)
                return null;

            string sql = "SELECT Top 1 * FROM ShopSinglePage WHERE Title=@Title And ShopId=@ShopId";
            var parameter = new IDataParameter[]
                                             {
                                                 this.RepositoryDriver.BuildParameter(ShopSinglePageFactory.FieldNames.Title,title),
                                                 this.RepositoryDriver.BuildParameter(ShopSinglePageFactory.FieldNames.ShopId,shop.Key)
                                                               
            };
            return BuildEntityFromSql(sql, parameter);
        }

        #endregion

        #region IShopSinglePageRepository 成员


        public void RemoveAll(Shop shop)
        {
            if (shop == null) return;
            var query = "DELETE FROM ShopSinglePage WHERE ShopId=@ShopId";
            var parameter = new IDataParameter[]
                                {
                                    this.RepositoryDriver.BuildParameter(ShopSinglePageFactory.FieldNames.ShopId,shop.Key),
                             
                                };
            this.BuildEntitiesFromSql(query, parameter);
        }

        #endregion
    }
}
